Configuration cInstallerWindowsService
{
    param (

	[Parameter(Mandatory)]
	[ValidateNotNullOrEmpty()]
	[PSObject]$ServiceDefinition
    )


$SetScriptBlock_Install=@"
Import-module Application.Management
Stop-WindowsService -Name '$($ServiceDefinition.ServiceName)' -ExpectedStartupType $($ServiceDefinition.StartupType)
Install-WindowsService -Name '$($ServiceDefinition.ServiceName)' -Path '$($ServiceDefinition.ExecutablePath)' -Arguments '$($ServiceDefinition.ExecutableArguments)'
"@

$SetScriptBlock_UnInstall=@"
Import-module Application.Management
Stop-WindowsService -Name '$($ServiceDefinition.ServiceName)' -ExpectedStartupType $($ServiceDefinition.StartupType)
UnInstall-WindowsService -Name '$($ServiceDefinition.ServiceName)'
"@


if ($ServiceDefinition.Ensure -eq 'Present')
{
    $SetScriptBlock = $SetScriptBlock_Install
}
else
{
    $SetScriptBlock = $SetScriptBlock_UnInstall
}


    #Log Message
    Log "cInstallerWindowsServiceStart_$($ServiceDefinition.uniqueKey)" {
        Message = "Start cInstallerWindowsService for $($ServiceDefinition.serviceName)"
    } 


    Script "cInstallerWindowsServiceScript_$($ServiceDefinition.uniqueKey)" {
        GetScript = "Import-module Application.Management; `$svcWindows = Get-WindowsServiceProperties -Name '$($ServiceDefinition.ServiceName)'; @{ Result = `$svcWindows}"
        SetScript = $SetScriptBlock
        # [scriptblock]::create("Install-Service -Name '$($ServiceDefinition.serviceName)' -Path $($ServiceDefinition.Path)");
        TestScript = "Import-module Application.Management; ((Get-WindowsServiceProperties -Name '$($ServiceDefinition.ServiceName)') -ne `$null)"
    }
    
    Service "Service_$($ServiceDefinition.uniqueKey)" {
        Name=$ServiceDefinition.ServiceName
        StartupType = $ServiceDefinition.StartupType
        State = $ServiceDefinition.State
        DependsOn = "[Script]cInstallerWindowsServiceScript_$($ServiceDefinition.uniqueKey)"
    }  

    #Log Message
    Log "cInstallerWindowsServiceEnd_$($ServiceDefinition.uniqueKey)" {
        Message = "End cInstallerWindowsService for $($ServiceDefinition.serviceName)"
    } 
}